const router = require('koa-router')();
let { verifyToken } = require('../utils/jsonwebtoken');
async function isLoginUser(ctx, next) {
    let token = ctx.request.headers.token
    let token_data = verifyToken(token);
    if (!token_data) {
        ctx.body = {
            success: false,
            msg: '身份信息认证失败'
        }
        return
    }
    await next()
}
async function paramsFilter(ctx, next) {
    let params = ctx.request.body;
    console.log('paramsFilter', params);
    await next();
}

module.exports = (app) => {
    router.get('/test', require('../test/mongoose').getData);
    // 用作拿后台的json数据
    router.get('/getJsonInfo', require('./bird').getSystemBirdInfo);
    // 主页天气数据接口
    router.get('/wether', require('./data').getWether);
    // 百度识图API 获取access_token
    router.get('/access_token', require('./data').getAccessToken);
    // 识图接口，传入图片url
    router.post('/getBirdByImgUrl', require('./data').getBirdByImgUrl)
    router.post('/searchbirdbybase64', require('./data').searchbirdByBase64)
    // 登陆
    router.post('/user/login', require('./user').login)
    // 注册
    router.post('/user/signup', require('./user').signup)
    // 自动登录校验用户token
    router.get('/user/verifyToken', require('./user').verifyToken)
    // 主页获取用户信息
    router.get('/user/getUserinfo', isLoginUser, require('./user').getUserInfo)
    // 根据ID查找用户信息
    router.post('/user/getUserInfoById', isLoginUser, require('./user').getUserInfoById)
    // 更新用户信息
    router.post('/user/update', require('./user').updateUserInfo)
    // 获取关注列表
    // router.post('/user/follower', require('./user').getFollower)
    // // 获取分析列表
    // router.post('/user/fans', require('./user').getFans) 
    // 发布笔记
    router.post('/note/release', isLoginUser, require('./note').release);
    // 获取所有笔记
    router.post('/getAllNoteByUserId', isLoginUser, require('./note').getAllNoteByUserId);
    // 根据笔记的ID获取笔记信息
    router.post('/note/getNoteDetail', isLoginUser, require('./note').getNoteById);
    // 根据笔记的ID删除笔记信息
    router.post('/note/deleteNoteById', isLoginUser, require('./note').deleteNoteById);

    //获取非己的全部note 
    router.post('/note/getAllNote', require('./note').getAllNote)
    // 发布留言
    router.post('/note/leavewords', isLoginUser, require('./note').leaveWords)
    // 关注
    router.post('/follower/addFollower', isLoginUser, require('./user').addFollower)
    // 取消关注
    router.post('/follower/cancelFollower', isLoginUser, require('./user').cancelFollower)
    // 点赞
    router.post('/note/likeNote', isLoginUser, require('./note').doLike);
    // 取消点赞
    router.post('/note/unlikeNote', isLoginUser, require('./note').doUnLike);

    router.post('/bird/addBird', isLoginUser, require('./bird').addBird);

    router.post('/bird/getBirdsByClassify', isLoginUser, require('./bird').getBirdsByClassify);

    router.post('/bird/getBirdinfo', isLoginUser, require('./bird').getBirdById);

    // router.post('/bird/search')
    router.post('/bird/fuzzyquery', isLoginUser, require('./bird').getFuzzyquery);

    // 文件上传功能接口
    router.post('/upload', require('./file').imgUpload);

    app.use(router.routes()).use(router.allowedMethods());
};
